package application;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;

import javax.xml.namespace.QName;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.stream.StreamResult;





import jlibs.xml.sax.XMLDocument;
import jlibs.xml.xsd.XSInstance;
import jlibs.xml.xsd.XSParser;

import org.apache.xerces.xs.*;


public class XSDToXML {

	final static String DIR_TMP = System.getProperty ("java.io.tmpdir")
			+ "\\Dynamic Mapper\\";
	
	public static boolean makeXmlModel(File file) {
		final XSModel xsModel = new XSParser().parse(file.getPath());
		final XSInstance xsInstance = new XSInstance();
		// xsInstance.minimumElementsGenerated = 2;
		// xsInstance.maximumElementsGenerated = 4;
		xsInstance.generateOptionalElements = Boolean.TRUE; // null means random
		final QName rootElement = new QName(
				"http://www.portalfiscal.inf.br/nfe", "NFe");
		XMLDocument sampleXml;
		try {
			
			StreamResult streamResult = new StreamResult(DIR_TMP+"model_xml.xml");
			sampleXml = new XMLDocument(streamResult, true, 4, null);
			sampleXml.suggestPrefix("", "http://www.portalfiscal.inf.br/nfe");
	
			xsInstance.generate(xsModel, rootElement, sampleXml);
		
			return true;
		} catch (TransformerConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} catch (IllegalArgumentException e) {
			return false;
		}
	}

	public static String getPathXmlModel() {
		return DIR_TMP + "\\model_xml.xml";

	}
}